import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// 开启压缩
import viteCompression from 'vite-plugin-compression'
import styleImport from 'vite-plugin-style-import'
import path from 'path'
import viteSvgIcons from 'vite-plugin-svg-icons'
import postcssImport from 'postcss-import'
import autoprefixer from 'autoprefixer'
export const resolve = (dir: string) => {
  const _path: string = path.resolve(__dirname, dir)
  return _path
}

export default defineConfig({
  plugins: [
    vue(),
    viteSvgIcons({
      // 指定需要缓存的图标文件夹
      iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
      // 指定symbolId格式
      symbolId: 'icon-[dir]-[name]'
    }),
    // gizp
    viteCompression(),
    // br
    viteCompression({
      ext: '.br',
      algorithm: 'brotliCompress'
    }),
    styleImport({
      libs: [
        {
          libraryName: 'vant',
          esModule: true,
          resolveStyle: (name) => {
            return `vant/es/${name}/style`
          }
        }
      ]
    })
  ],
  resolve: {
    alias: {
      '@': resolve('src'),
      '@comp': resolve('./src/components'),
      '@views': resolve('./src/views'),
      '@utils': resolve('./src/views')
    }
  },
  build: {
    terserOptions: {
      compress: {
        // 生产环境移除 console
        drop_console: true,
        drop_debugger: true
      }
    }
  },
  css: {
    postcss: {
      plugins: [postcssImport as any, autoprefixer]
    },
    preprocessorOptions: {
      // less: {
      //   modifyVars: {
      //     hack: `true; @import (reference) "${resolve('src/assets/css/common.less')}";`
      //   },
      //   javascriptEnabled: true
      // },

      scss: {
        additionalData: '@import "./src/assets/css/common.scss";' //引用公共样式，使用vite搭建项目只安装sass即可，不需要安装node-sass,sass-loader
      }
    }
  }
})
